package com.ruiysoft.controller.order;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruiysoft.controller.order.api.ShoporderApi;
import com.ruiysoft.controller.order.util.OrderShopUtil;
import com.ruiysoft.controller.order.util.OrderWechatUtil;
import com.ruiysoft.controller.order.vo.OrderDeliverVo;
import com.ruiysoft.controller.order.vo.OrderListCusVo;
import com.ruiysoft.controller.order.vo.OrderModifyVo;
import com.ruiysoft.controller.order.vo.SplitOrderVO;
import com.ruiysoft.entity.ModelApiResponse;
import com.ruiysoft.pub.core.Result;
import com.ruiysoft.pub.core.ResultGenerator;
import com.ruiysoft.pub.entity.PageInfo;
import com.ruiysoft.service.order.IOrderShopService;
import com.ruiysoft.service.order.bean.OrderDto;
import com.ruiysoft.service.sys.clent.LoginContextUtil;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2018-11-27T16:57:35.761+08:00")

@RestController
public class ShoporderApiController implements ShoporderApi {

    private static final Logger log = LoggerFactory.getLogger(ShoporderApiController.class);

    private final ObjectMapper objectMapper;

    private final IOrderShopService orderShopService;

    @org.springframework.beans.factory.annotation.Autowired
    public ShoporderApiController(ObjectMapper objectMapper, IOrderShopService orderShopService) {
        this.objectMapper = objectMapper;
        this.orderShopService = orderShopService;
    }

    private Long getCurrShopId() {
        return LoginContextUtil.instance().getEntryMicroShop().getId();
    }

    @Override
    public Result<ModelApiResponse> shoporderDeliverPost(
            @ApiParam(value = "订单信息") @Valid @RequestBody OrderDeliverVo orderDeliverVo
    ) {
        return orderShopService.orderDeliver(getCurrShopId(), orderDeliverVo);
    }

    @Override
    public Result<ModelApiResponse> shopOrderSplitPost(@ApiParam(value = "订单ID") @Valid List<SplitOrderVO> splitOrderVOS) {
        return orderShopService.shopOrderSplit(getCurrShopId(), splitOrderVOS);
    }

    @Override
    public Result<ModelApiResponse> shopOrderMergePost(@Valid SplitOrderVO splitOrderVOS) {
        return orderShopService.shopOrderMergePost(getCurrShopId(), splitOrderVOS);
    }

//    @Cacheable(value = "shoporderOrderlistGet")
    @Override
    public Result<PageInfo<OrderListCusVo>> shoporderOrderlistGet(
            @NotNull @ApiParam(value = "分页页码", required = true) @Valid @RequestParam(value = "pageStart", required = true) Integer pageStart,
            @NotNull @ApiParam(value = "每页条数", required = true) @Valid @RequestParam(value = "pageSize", required = true) Integer pageSize,
            @ApiParam(value = "订单状态值") @Valid @RequestParam(value = "status", required = false) String status
    ) {

        String[] statusArr = Optional.ofNullable(status).map(s -> s.split(",")).orElse(null);
        Set<Integer> statusSet = Optional.ofNullable(statusArr)
                .map(sarr ->
                        Arrays.stream(sarr)
                                .filter(StringUtils::isNotBlank)
                                .map(s -> Integer.valueOf(s.trim()))
                                .collect(Collectors.toSet()))
                .orElse(null);

        PageInfo<OrderDto> dtos = orderShopService.findOrders(getCurrShopId(), statusSet, pageStart, pageSize);
        List<OrderListCusVo> vos = OrderWechatUtil.INSTANCE.orderDtos2OrderListCusVos(dtos.getList());
        return ResultGenerator.genSuccessResult(new PageInfo<OrderListCusVo>(vos, dtos));
    }

    @Override
    public Result shoporderModifyPost(
            @ApiParam(value = "订单金额修订信息") @Valid @RequestBody OrderModifyVo orderModifyVo) {
        return orderShopService.updateOrderMoney(getCurrShopId(), orderModifyVo);
    }

    @Override
    public Result<PageInfo<OrderListCusVo>> shoporderWaitingdeliveringGet(
            @NotNull @ApiParam(value = "分页页码", required = true) @Valid @RequestParam(value = "pageStart", required = true) Integer pageStart,
            @NotNull @ApiParam(value = "每页条数", required = true) @Valid @RequestParam(value = "pageSize", required = true) Integer pageSize
    ) {
        PageInfo<OrderDto> page = orderShopService.findOrdersWaitingDelivering(getCurrShopId(), pageStart, pageSize);
        List<OrderListCusVo> vos = OrderShopUtil.INSTANCE.orderDtos2OrderListCusVos(page.getList());
        return ResultGenerator.genSuccessResult(new PageInfo<OrderListCusVo>(vos, page));
    }
}
